package com.opNaga.bpmp.user;

import com.opNaga.bpmp.BPMPElement;
import com.opNaga.bpmp.requirement.AbilityRequirement;
import com.opNaga.bpmp.requirement.NetworkRequirement;
import com.opNaga.bpmp.requirement.PositionRequirement;

import java.util.*;

/**
 * User: Junyi BEI
 * Date: 01/02/2012
 * Time: 15:27
 */
public class User extends BPMPElement {
  private Set<BPMPElement> abilities = new HashSet<BPMPElement>();
  private Set<BPMPElement> networks = new HashSet<BPMPElement>();
  private Set<BPMPElement> positions = new HashSet<BPMPElement>();

  public User() {
    super();
  }
  
  public User(String n) {
    super(n);
  }

  @Override
  public String getType() {
    return "User";
  }

  public boolean addAbility(BPMPElement bc) {
    if(!AbilityRequirement.class.isAssignableFrom(bc.getClass())) {
      return standardMessage(1000, bc);
    }
    if(!abilities.add(bc)) {
      return standardMessage(1001, bc);
    }
    return standardMessage(1008, bc);
  }

  public boolean addAbilities(BPMPElement... bes) {
    boolean allSucceed = true;
    for(BPMPElement be : bes) {
      allSucceed &= addAbility(be);
    }
    return allSucceed;
  }

  public boolean removeAbility(BPMPElement bc) {
    if(!AbilityRequirement.class.isAssignableFrom(bc.getClass())) {
      return standardMessage(1002, bc);
    }
    if(!abilities.remove(bc)) {
      return standardMessage(1003, bc);
    }
    return standardMessage(1009, bc);
  }

  public Iterator<BPMPElement> iterateAbilities() {
    return new ArrayList<BPMPElement>(abilities).iterator();
  }

  public boolean addNetwork(BPMPElement bc) {
    if(!NetworkRequirement.class.isAssignableFrom(bc.getClass())) {
      return standardMessage(1020, bc);
    }
    if(!networks.add(bc)) {
      return standardMessage(1021, bc);
    }
    return standardMessage(1028, bc);
  }

  public boolean addNetworks(BPMPElement... bes) {
    boolean allSucceed = true;
    for(BPMPElement be : bes) {
      allSucceed &= addNetwork(be);
    }
    return allSucceed;
  }

  public boolean removeNetwork(BPMPElement bc) {
    if(!NetworkRequirement.class.isAssignableFrom(bc.getClass())) {
      return standardMessage(1022, bc);
    }
    if(!networks.remove(bc)) {
      return standardMessage(1023, bc);
    }
    return standardMessage(1029, bc);
  }

  public Iterator<BPMPElement> iterateNetworks() {
    return new ArrayList<BPMPElement>(networks).iterator();
  }

  public boolean addPosition(BPMPElement bc) {
    if(!PositionRequirement.class.isAssignableFrom(bc.getClass())) {
      return standardMessage(1010, bc);
    }
    if(!positions.add(bc)) {
      return standardMessage(1011, bc);
    }
    return standardMessage(1018, bc);
  }

  public boolean addPositions(BPMPElement... bes) {
    boolean allSucceed = true;
    for(BPMPElement be : bes) {
      allSucceed &= addPosition(be);
    }
    return allSucceed;
  }

  public boolean removePosition(BPMPElement bc) {
    if(!PositionRequirement.class.isAssignableFrom(bc.getClass())) {
      return standardMessage(1012, bc);
    }
    if(!positions.remove(bc)) {
      return standardMessage(1013, bc);
    }
    return standardMessage(1019, bc);
  }

  public Iterator<BPMPElement> iteratePositions() {
    return new ArrayList<BPMPElement>(positions).iterator();
  }
}
